Emphasis and strong emphasis

John Gruber’s original Markdown syntax description says:

Markdown treats asterisks (*) and underscores (_) as indicators of emphasis. Text wrapped with one * or _ will be wrapped with an HTML <em> tag; double *’s or _’s will be wrapped with an HTML <strong> tag.

This is enough for most users, but these rules leave much undecided, especially when it comes to nested emphasis. The original Markdown.pl test suite makes it clear that triple *** and ___ delimiters can be used for strong emphasis, and most implementations have also allowed the following patterns:

***strong emph***
***strong** in emph*
***emph* in strong**
**in strong *emph***
*in emph **strong***

The following patterns are less widely supported, but the intent is clear and they are useful (especially in contexts like bibliography entries):

*emph *with emph* in it*
**strong **with strong** in it**

Many implementations have also restricted intraword emphasis to the * forms, to avoid unwanted emphasis in words containing internal underscores. (It is best practice to put these in code spans, but users often do not.)

internal emphasis: foo*bar*baz
no emphasis: foo_bar_baz

The rules given below capture all of these patterns, while allowing for efficient parsing strategies that do not backtrack.
First, some definitions. A delimiter run is either a sequence of one or more * characters that is not preceded or followed by a non-backslash-escaped * character, or a sequence of one or more _ characters that is not preceded or followed by a non-backslash-escaped _ character.
left-flanking delimiter run is a delimiter run that is (1) not followed by Unicode whitespace, and either (2a) not followed by a punctuation character,or (2b) followed by a punctuation character and preceded by Unicode whitespace or a punctuation character. For purposes of this definition, the beginning and the end of the line count as Unicode whitespace.
right-flanking delimiter run is a delimiter run that is (1) not preceded by Unicode whitespace, and either (2a) not preceded by a punctuation character,or (2b) preceded by a punctuation character and followed by Unicode whitespace or a punctuation character. For purposes of this definition, the beginning and the end of the line count as Unicode whitespace.
Here are some examples of delimiter runs.

  • left-flanking but not right-flanking:

    ***abc
      _abc
    **"abc"
     _"abc"
    
  • right-flanking but not left-flanking:

     abc***
     abc_
    "abc"**
    "abc"_
    
  • Both left and right-flanking:

     abc***def
    "abc"_"def"
    
  • Neither left nor right-flanking:

    abc *** def
    a _ b
    

(The idea of distinguishing left-flanking and right-flanking delimiter runs based on the character before and the character after comes from Roopesh Chander’s vfmd. vfmd uses the terminology “emphasis indicator string” instead of “delimiter run,” and its rules for distinguishing left- and right-flanking runs are a bit more complex than the ones given here.)
The following rules define emphasis and strong emphasis:

  1. A single * character can open emphasis iff (if and only if) it is part of a left-flanking delimiter run.
  2. A single _ character can open emphasis iff it is part of a left-flanking delimiter run and either (a) not part of a right-flanking delimiter run or (b) part of a right-flanking delimiter run preceded by punctuation.
  3. A single * character can close emphasis iff it is part of a right-flanking delimiter run.
  4. A single _ character can close emphasis iff it is part of a right-flanking delimiter run and either (a) not part of a left-flanking delimiter run or (b) part of a left-flanking delimiter run followed by punctuation.
  5. A double ** can open strong emphasis iff it is part of a left-flanking delimiter run.
  6. A double __ can open strong emphasis iff it is part of a left-flanking delimiter run and either (a) not part of a right-flanking delimiter run or (b) part of a right-flanking delimiter run preceded by punctuation.
  7. A double ** can close strong emphasis iff it is part of a right-flanking delimiter run.
  8. A double __ can close strong emphasis iff it is part of a right-flanking delimiter run and either (a) not part of a left-flanking delimiter run or (b) part of a left-flanking delimiter run followed by punctuation.
  9. Emphasis begins with a delimiter that can open emphasis and ends with a delimiter that can close emphasis, and that uses the same character (_ or *) as the opening delimiter. The opening and closing delimiters must belong to separate delimiter runs. If one of the delimiters can both open and close emphasis, then the sum of the lengths of the delimiter runs containing the opening and closing delimiters must not be a multiple of 3 unless both lengths are multiples of 3.
  10. Strong emphasis begins with a delimiter that can open strong emphasis and ends with a delimiter thatcan close strong emphasis, and that uses the same character (_ or *) as the opening delimiter. The opening and closing delimiters must belong to separate delimiter runs. If one of the delimiters can both open and close strong emphasis, then the sum of the lengths of the delimiter runs containing the opening and closing delimiters must not be a multiple of 3 unless both lengths are multiples of 3.
  11. A literal * character cannot occur at the beginning or end of *-delimited emphasis or **-delimited strong emphasis, unless it is backslash-escaped.
  12. A literal _ character cannot occur at the beginning or end of _-delimited emphasis or __-delimited strong emphasis, unless it is backslash-escaped.

Where rules 1–12 above are compatible with multiple parsings, the following principles resolve ambiguity:

  1. The number of nestings should be minimized. Thus, for example, an interpretation <strong>...</strong> is always preferred to <em><em>...</em></em>.
  2. An interpretation <em><strong>...</strong></em> is always preferred to <strong><em>...</em></strong>.
  3. When two potential emphasis or strong emphasis spans overlap, so that the second begins before the first ends and ends after the first ends, the first takes precedence. Thus, for example, *foo _bar* baz_ is parsed as <em>foo _bar</em> baz_ rather than *foo <em>bar* baz</em>.
  4. When there are two potential emphasis or strong emphasis spans with the same closing delimiter, the shorter one (the one that opens later) takes precedence. Thus, for example, **foo **bar baz** is parsed as **foo <strong>bar baz</strong> rather than <strong>foo **bar baz</strong>.
  5. Inline code spans, links, images, and HTML tags group more tightly than emphasis. So, when there is a choice between an interpretation that contains one of these elements and one that does not, the former always wins. Thus, for example, *[foo*](bar) is parsed as *<a href="bar">foo*</a> rather than as <em>[foo</em>](bar).

These rules can be illustrated through a series of examples.
Rule 1:

Example 360

Markdown HTML Demo
*foo bar*

<p><em>foo bar</em></p>

This is not emphasis, because the opening * is followed by whitespace, and hence not part of a left-flanking delimiter run:

Example 361

Markdown HTML Demo
a * foo bar*

<p>a * foo bar*</p>

This is not emphasis, because the opening * is preceded by an alphanumeric and followed by punctuation, and hence not part of a left-flanking delimiter run:

Example 362

Markdown HTML Demo
a*"foo"*

<p>a*&quot;foo&quot;*</p>

Unicode nonbreaking spaces count as whitespace, too:

Example 363

Markdown HTML Demo
* a *

<p>* a *</p>

Intraword emphasis with * is permitted:

Example 364

Markdown HTML Demo
foo*bar*

<p>foo<em>bar</em></p>

Example 365

Markdown HTML Demo
5*6*78

<p>5<em>6</em>78</p>

Rule 2:

Example 366

Markdown HTML Demo
_foo bar_

<p><em>foo bar</em></p>

This is not emphasis, because the opening _ is followed by whitespace:

Example 367

Markdown HTML Demo
_ foo bar_

<p>_ foo bar_</p>

This is not emphasis, because the opening _ is preceded by an alphanumeric and followed by punctuation:

Example 368

Markdown HTML Demo
a_"foo"_

<p>a_&quot;foo&quot;_</p>

Emphasis with _ is not allowed inside words:

Example 369

Markdown HTML Demo
foo_bar_

<p>foo_bar_</p>

Example 370

Markdown HTML Demo
5_6_78

<p>5_6_78</p>

Example 371

Markdown HTML Demo
пристаням_стремятся_

<p>пристаням_стремятся_</p>

Here _ does not generate emphasis, because the first delimiter run is right-flanking and the second left-flanking:

Example 372

Markdown HTML Demo
aa_"bb"_cc

<p>aa_&quot;bb&quot;_cc</p>

This is emphasis, even though the opening delimiter is both left- and right-flanking, because it is preceded by punctuation:

Example 373

Markdown HTML Demo
foo-_(bar)_

<p>foo-<em>(bar)</em></p>

Rule 3:
This is not emphasis, because the closing delimiter does not match the opening delimiter:

Example 374

Markdown HTML Demo
_foo*

<p>_foo*</p>

This is not emphasis, because the closing * is preceded by whitespace:

Example 375

Markdown HTML Demo
*foo bar *

<p>*foo bar *</p>

A newline also counts as whitespace:

Example 376

Markdown HTML Demo
*foo bar
*

<p>*foo bar
*</p>

This is not emphasis, because the second * is preceded by punctuation and followed by an alphanumeric (hence it is not part of a right-flanking delimiter run:

Example 377

Markdown HTML Demo
*(*foo)

<p>*(*foo)</p>

The point of this restriction is more easily appreciated with this example:

Example 378

Markdown HTML Demo
*(*foo*)*

<p><em>(<em>foo</em>)</em></p>

Intraword emphasis with * is allowed:

Example 379

Markdown HTML Demo
*foo*bar

<p><em>foo</em>bar</p>

Rule 4:
This is not emphasis, because the closing _ is preceded by whitespace:

Example 380

Markdown HTML Demo
_foo bar _

<p>_foo bar _</p>

This is not emphasis, because the second _ is preceded by punctuation and followed by an alphanumeric:

Example 381

Markdown HTML Demo
_(_foo)

<p>_(_foo)</p>

This is emphasis within emphasis:

Example 382

Markdown HTML Demo
_(_foo_)_

<p><em>(<em>foo</em>)</em></p>

Intraword emphasis is disallowed for _:

Example 383

Markdown HTML Demo
_foo_bar

<p>_foo_bar</p>

Example 384

Markdown HTML Demo
_пристаням_стремятся

<p>_пристаням_стремятся</p>

Example 385

Markdown HTML Demo
_foo_bar_baz_

<p><em>foo_bar_baz</em></p>

This is emphasis, even though the closing delimiter is both left- and right-flanking, because it is followed by punctuation:

Example 386

Markdown HTML Demo
_(bar)_.

<p><em>(bar)</em>.</p>

Rule 5:

Example 387

Markdown HTML Demo
**foo bar**

<p><strong>foo bar</strong></p>

This is not strong emphasis, because the opening delimiter is followed by whitespace:

Example 388

Markdown HTML Demo
** foo bar**

<p>** foo bar**</p>

This is not strong emphasis, because the opening ** is preceded by an alphanumeric and followed by punctuation, and hence not part of a left-flanking delimiter run:

Example 389

Markdown HTML Demo
a**"foo"**

<p>a**&quot;foo&quot;**</p>

Intraword strong emphasis with ** is permitted:

Example 390

Markdown HTML Demo
foo**bar**

<p>foo<strong>bar</strong></p>

Rule 6:

Example 391

Markdown HTML Demo
__foo bar__

<p><strong>foo bar</strong></p>

This is not strong emphasis, because the opening delimiter is followed by whitespace:

Example 392

Markdown HTML Demo
__ foo bar__

<p>__ foo bar__</p>

A newline counts as whitespace:

Example 393

Markdown HTML Demo
__
foo bar__

<p>__
foo bar__</p>

This is not strong emphasis, because the opening __ is preceded by an alphanumeric and followed by punctuation:

Example 394

Markdown HTML Demo
a__"foo"__

<p>a__&quot;foo&quot;__</p>

Intraword strong emphasis is forbidden with __:

Example 395

Markdown HTML Demo
foo__bar__

<p>foo__bar__</p>

Example 396

Markdown HTML Demo
5__6__78

<p>5__6__78</p>

Example 397

Markdown HTML Demo
пристаням__стремятся__

<p>пристаням__стремятся__</p>

Example 398

Markdown HTML Demo
__foo, __bar__, baz__

<p><strong>foo, <strong>bar</strong>, baz</strong></p>

This is strong emphasis, even though the opening delimiter is both left- and right-flanking, because it is preceded by punctuation:

Example 399

Markdown HTML Demo
foo-__(bar)__

<p>foo-<strong>(bar)</strong></p>

Rule 7:
This is not strong emphasis, because the closing delimiter is preceded by whitespace:

Example 400

Markdown HTML Demo
**foo bar **

<p>**foo bar **</p>

(Nor can it be interpreted as an emphasized *foo bar *, because of Rule 11.)
This is not strong emphasis, because the second ** is preceded by punctuation and followed by an alphanumeric:

Example 401

Markdown HTML Demo
**(**foo)

<p>**(**foo)</p>

The point of this restriction is more easily appreciated with these examples:

Example 402

Markdown HTML Demo
*(**foo**)*

<p><em>(<strong>foo</strong>)</em></p>

Example 403

Markdown HTML Demo
**Gomphocarpus (*Gomphocarpus physocarpus*, syn.
*Asclepias physocarpa*)**

<p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
<em>Asclepias physocarpa</em>)</strong></p>

Example 404

Markdown HTML Demo
**foo "*bar*" foo**

<p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>

Intraword emphasis:

Example 405

Markdown HTML Demo
**foo**bar

<p><strong>foo</strong>bar</p>

Rule 8:
This is not strong emphasis, because the closing delimiter is preceded by whitespace:

Example 406

Markdown HTML Demo
__foo bar __

<p>__foo bar __</p>

This is not strong emphasis, because the second __ is preceded by punctuation and followed by an alphanumeric:

Example 407

Markdown HTML Demo
__(__foo)

<p>__(__foo)</p>

The point of this restriction is more easily appreciated with this example:

Example 408

Markdown HTML Demo
_(__foo__)_

<p><em>(<strong>foo</strong>)</em></p>

Intraword strong emphasis is forbidden with __:

Example 409

Markdown HTML Demo
__foo__bar

<p>__foo__bar</p>

Example 410

Markdown HTML Demo
__пристаням__стремятся

<p>__пристаням__стремятся</p>

Example 411

Markdown HTML Demo
__foo__bar__baz__

<p><strong>foo__bar__baz</strong></p>

This is strong emphasis, even though the closing delimiter is both left- and right-flanking, because it is followed by punctuation:

Example 412

Markdown HTML Demo
__(bar)__.

<p><strong>(bar)</strong>.</p>

Rule 9:
Any nonempty sequence of inline elements can be the contents of an emphasized span.

Example 413

Markdown HTML Demo
*foo [bar](/url)*

<p><em>foo <a href="/url">bar</a></em></p>

Example 414

Markdown HTML Demo
*foo
bar*

<p><em>foo
bar</em></p>

In particular, emphasis and strong emphasis can be nested inside emphasis:

Example 415

Markdown HTML Demo
_foo __bar__ baz_

<p><em>foo <strong>bar</strong> baz</em></p>

Example 416

Markdown HTML Demo
_foo _bar_ baz_

<p><em>foo <em>bar</em> baz</em></p>

Example 417

Markdown HTML Demo
__foo_ bar_

<p><em><em>foo</em> bar</em></p>

Example 418

Markdown HTML Demo
*foo *bar**

<p><em>foo <em>bar</em></em></p>

Example 419

Markdown HTML Demo
*foo **bar** baz*

<p><em>foo <strong>bar</strong> baz</em></p>

Example 420

Markdown HTML Demo
*foo**bar**baz*

<p><em>foo<strong>bar</strong>baz</em></p>

Note that in the preceding case, the interpretation

<p><em>foo</em><em>bar<em></em>baz</em></p>

is precluded by the condition that a delimiter that can both open and close (like the * after foo) cannot form emphasis if the sum of the lengths of the delimiter runs containing the opening and closing delimiters is a multiple of 3 unless both lengths are multiples of 3.

For the same reason, we don’t get two consecutive emphasis sections in this example:

Example 421

Markdown HTML Demo
*foo**bar*

<p><em>foo**bar</em></p>

The same condition ensures that the following cases are all strong emphasis nested inside emphasis, even when the interior spaces are omitted:

Example 422

Markdown HTML Demo
***foo** bar*

<p><em><strong>foo</strong> bar</em></p>

Example 423

Markdown HTML Demo
*foo **bar***

<p><em>foo <strong>bar</strong></em></p>

Example 424

Markdown HTML Demo
*foo**bar***

<p><em>foo<strong>bar</strong></em></p>

When the lengths of the interior closing and opening delimiter runs are both multiples of 3, though, they can match to create emphasis:

Example 425

Markdown HTML Demo
foo***bar***baz

<p>foo<em><strong>bar</strong></em>baz</p>

Example 426

Markdown HTML Demo
foo******bar*********baz

<p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>

Indefinite levels of nesting are possible:

Example 427

Markdown HTML Demo
*foo **bar *baz* bim** bop*

<p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>

Example 428

Markdown HTML Demo
*foo [*bar*](/url)*

<p><em>foo <a href="/url"><em>bar</em></a></em></p>

There can be no empty emphasis or strong emphasis:

Example 429

Markdown HTML Demo
** is not an empty emphasis

<p>** is not an empty emphasis</p>

Example 430

Markdown HTML Demo
**** is not an empty strong emphasis

<p>**** is not an empty strong emphasis</p>

Rule 10:
Any nonempty sequence of inline elements can be the contents of an strongly emphasized span.

Example 431

Markdown HTML Demo
**foo [bar](/url)**

<p><strong>foo <a href="/url">bar</a></strong></p>

Example 432

Markdown HTML Demo
**foo
bar**

<p><strong>foo
bar</strong></p>

In particular, emphasis and strong emphasis can be nested inside strong emphasis:

Example 433

Markdown HTML Demo
__foo _bar_ baz__

<p><strong>foo <em>bar</em> baz</strong></p>

Example 434

Markdown HTML Demo
__foo __bar__ baz__

<p><strong>foo <strong>bar</strong> baz</strong></p>

Example 435

Markdown HTML Demo
____foo__ bar__

<p><strong><strong>foo</strong> bar</strong></p>

Example 436

Markdown HTML Demo
**foo **bar****

<p><strong>foo <strong>bar</strong></strong></p>

Example 437

Markdown HTML Demo
**foo *bar* baz**

<p><strong>foo <em>bar</em> baz</strong></p>

Example 438

Markdown HTML Demo
**foo*bar*baz**

<p><strong>foo<em>bar</em>baz</strong></p>

Example 439

Markdown HTML Demo
***foo* bar**

<p><strong><em>foo</em> bar</strong></p>

Example 440

Markdown HTML Demo
**foo *bar***

<p><strong>foo <em>bar</em></strong></p>

Indefinite levels of nesting are possible:

Example 441

Markdown HTML Demo
**foo *bar **baz**
bim* bop**

<p><strong>foo <em>bar <strong>baz</strong>
bim</em> bop</strong></p>

Example 442

Markdown HTML Demo
**foo [*bar*](/url)**

<p><strong>foo <a href="/url"><em>bar</em></a></strong></p>

There can be no empty emphasis or strong emphasis:

Example 443

Markdown HTML Demo
__ is not an empty emphasis

<p>__ is not an empty emphasis</p>

Example 444

Markdown HTML Demo
____ is not an empty strong emphasis

<p>____ is not an empty strong emphasis</p>

Rule 11:

Example 445

Markdown HTML Demo
foo ***

<p>foo ***</p>

Example 446

Markdown HTML Demo
foo *\**

<p>foo <em>*</em></p>

Example 447

Markdown HTML Demo
foo *_*

<p>foo <em>_</em></p>

Example 448

Markdown HTML Demo
foo *****

<p>foo *****</p>

Example 449

Markdown HTML Demo
foo **\***

<p>foo <strong>*</strong></p>

Example 450

Markdown HTML Demo
foo **_**

<p>foo <strong>_</strong></p>

Note that when delimiters do not match evenly, Rule 11 determines that the excess literal * characters will appear outside of the emphasis, rather than inside it:

Example 451

Markdown HTML Demo
**foo*

<p>*<em>foo</em></p>

Example 452

Markdown HTML Demo
*foo**

<p><em>foo</em>*</p>

Example 453

Markdown HTML Demo
***foo**

<p>*<strong>foo</strong></p>

Example 454

Markdown HTML Demo
****foo*

<p>***<em>foo</em></p>

Example 455

Markdown HTML Demo
**foo***

<p><strong>foo</strong>*</p>

Example 456

Markdown HTML Demo
*foo****

<p><em>foo</em>***</p>

Rule 12:

Example 457

Markdown HTML Demo
foo ___

<p>foo ___</p>

Example 458

Markdown HTML Demo
foo _\__

<p>foo <em>_</em></p>

Example 459

Markdown HTML Demo
foo _*_

<p>foo <em>*</em></p>

Example 460

Markdown HTML Demo
foo _____

<p>foo _____</p>

Example 461

Markdown HTML Demo
foo __\___

<p>foo <strong>_</strong></p>

Example 462

Markdown HTML Demo
foo __*__

<p>foo <strong>*</strong></p>

Example 463

Markdown HTML Demo
__foo_

<p>_<em>foo</em></p>

Note that when delimiters do not match evenly, Rule 12 determines that the excess literal _ characters will appear outside of the emphasis, rather than inside it:

Example 464

Markdown HTML Demo
_foo__

<p><em>foo</em>_</p>

Example 465

Markdown HTML Demo
___foo__

<p>_<strong>foo</strong></p>

Example 466

Markdown HTML Demo
____foo_

<p>___<em>foo</em></p>

Example 467

Markdown HTML Demo
__foo___

<p><strong>foo</strong>_</p>

Example 468

Markdown HTML Demo
_foo____

<p><em>foo</em>___</p>

Rule 13 implies that if you want emphasis nested directly inside emphasis, you must use different delimiters:

Example 469

Markdown HTML Demo
**foo**

<p><strong>foo</strong></p>

Example 470

Markdown HTML Demo
*_foo_*

<p><em><em>foo</em></em></p>

Example 471

Markdown HTML Demo
__foo__

<p><strong>foo</strong></p>

Example 472

Markdown HTML Demo
_*foo*_

<p><em><em>foo</em></em></p>

However, strong emphasis within strong emphasis is possible without switching delimiters:

Example 473

Markdown HTML Demo
****foo****

<p><strong><strong>foo</strong></strong></p>

Example 474

Markdown HTML Demo
____foo____

<p><strong><strong>foo</strong></strong></p>

Rule 13 can be applied to arbitrarily long sequences of delimiters:

Example 475

Markdown HTML Demo
******foo******

<p><strong><strong><strong>foo</strong></strong></strong></p>

Rule 14:

Example 476

Markdown HTML Demo
***foo***

<p><em><strong>foo</strong></em></p>

Example 477

Markdown HTML Demo
_____foo_____

<p><em><strong><strong>foo</strong></strong></em></p>

Rule 15:

Example 478

Markdown HTML Demo
*foo _bar* baz_

<p><em>foo _bar</em> baz_</p>

Example 479

Markdown HTML Demo
*foo __bar *baz bim__ bam*

<p><em>foo <strong>bar *baz bim</strong> bam</em></p>

Rule 16:

Example 480

Markdown HTML Demo
**foo **bar baz**

<p>**foo <strong>bar baz</strong></p>

Example 481

Markdown HTML Demo
*foo *bar baz*

<p>*foo <em>bar baz</em></p>

Rule 17:

Example 482

Markdown HTML Demo
*[bar*](/url)

<p>*<a href="/url">bar*</a></p>

Example 483

Markdown HTML Demo
_foo [bar_](/url)

<p>_foo <a href="/url">bar_</a></p>

Example 484

Markdown HTML Demo
*<img src="foo" title="*"/>

<p>*<img src="foo" title="*"/></p>

Example 485

Markdown HTML Demo
**<a href="**">

<p>**<a href="**"></p>

Example 486

Markdown HTML Demo
__<a href="__">

<p>__<a href="__"></p>

Example 487

Markdown HTML Demo
*a `*`*

<p><em>a <code>*</code></em></p>

Example 488

Markdown HTML Demo
_a `_`_

<p><em>a <code>_</code></em></p>

Example 489

Markdown HTML Demo
**a<http://foo.bar/?q=**>

<p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>

Example 490

Markdown HTML Demo
__a<http://foo.bar/?q=__>

<p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>